/*
 * @Author       : Bing Chen chenbing@iocharger.com
 * @Date         : 2024-05-20 16:50:35
 * @LastEditors  : Bing Chen chenbing@iocharger.com
 * @LastEditTime : 2025-03-15 12:56:34
 * @FilePath     : \openchaoji-corelib\shared\include\OpenChaoJi_os.h
 * @Description  : 本文件定义系统通用方法
 *
 * Copyright (c) 2025 OpenChaoJi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the icense for the specific language governing permissions and
 * limitations under the License.
 *
 */

#ifndef __OPEN_CHAOJI_OS_H__
#define __OPEN_CHAOJI_OS_H__

#include "OpenChaoJi_config.h"
#include "OpenChaoJi_type.h"

/**
 * @brief: 获取当前系统已运行的时间, 单位毫秒ms
 * @return 已运行时间
 */
// TODO: 49天溢出的处理, 需要单独编写一个文档说明
uint32_t OpenChaoJi_os_get_ticks(void);

/**
 * @brief: 获取系统当前UTC日期时间, 原则上系统的计时应当使用ticks, 这个time仅用于报文需要互相传递实际时间戳
 * @return UTC时间戳
 */
time_t OpenChaoJi_os_get_time(void);

/**
 * 一般建议直接使用libc的内存管理库方法
*/
#if (OPENCHAOJI_CONFIG_MEMORY_MANAGER == OPENCHAOJI_CONFIG_MEMORY_STD_LIBC)
#define OpenChaoJi_os_calloc    calloc
#define OpenChaoJi_os_free      free
#define OpenChaoJi_os_malloc    malloc

#define OpenChaoJi_os_memeory_init()
#define OpenChaoJi_os_memeory_term()

#else

void * OpenChaoJi_os_calloc(size_t _NumOfElements,size_t _SizeOfElements);
void OpenChaoJi_os_free(void *_Memory);
void * OpenChaoJi_os_malloc(size_t _Size);

bool OpenChaoJi_os_memeory_init();
void OpenChaoJi_os_memeory_term();

#endif

/**
 * 内存释放检测处理
*/
// #define OPEN_CHAOJI_MEM_FREE_CHECK(pMem)	{((pMem)==NULL)?(NULL):(OpenChaoJi_os_free(pMem));(pMem)=(NULL);}

#define OPEN_CHAOJI_MEM_FREE_CHECK(pMem)	{\
                                                if (pMem != NULL){\
                                                    OpenChaoJi_os_free(pMem);\
                                                    pMem = NULL;\
                                                }\
                                            }

/**
 * 定义通用的软件计时器
*/
struct OpenChaoJi_os_soft_timer {
    uint32_t timeout_ms;        // 周期
    // uint32_t repeat_times;      // 重复次数  241104 根据讨论, timer都是由App层通过timeout到期reset来调用, 这里不再提供repeat 否则需要提供回调函数

    uint32_t start_ticks;       // 开始时间戳
    u8       started;           // 是否已开始
};

void OpenChaoJi_os_soft_timer_start(struct OpenChaoJi_os_soft_timer * timer, uint32_t timeout_ms);
void OpenChaoJi_os_soft_timer_stop(struct OpenChaoJi_os_soft_timer * timer);
void OpenChaoJi_os_soft_timer_reset(struct OpenChaoJi_os_soft_timer * timer);
int  OpenChaoJi_os_soft_timer_is_stopped(struct OpenChaoJi_os_soft_timer * timer);
int  OpenChaoJi_os_soft_timer_is_timeout(struct OpenChaoJi_os_soft_timer * timer);


#endif

